package com.hyy.book2.controller;

import com.hyy.book2.constants.Constants;

import com.hyy.book2.mapper.BookMapper;
import com.hyy.book2.model.BookInfo;
import com.hyy.book2.model.PageRequest;
import com.hyy.book2.model.PageResponse;
import com.hyy.book2.model.Result;
import com.hyy.book2.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;
    @Autowired
    private BookMapper bookMapper;

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("开始参数校验");
        if (bookInfo == null ||
                !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || (bookInfo.getCount() == null)
                || (bookInfo.getPrice() == null)
                || !StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数不合法，请重新检查！";
        }
        return bookService.InsertBook(bookInfo);
    }

    @RequestMapping("/getListByPage")
    public Result<PageResponse<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session) {

        log.info("检验是否登录");
        //强制登陆
        if (session == null
                || session.getAttribute(Constants.SESSION_USER_YEY) == null
                || !StringUtils.hasLength((String) session.getAttribute(Constants.SESSION_USER_YEY))) {
            return Result.unlogin();
        }


        log.info("检查前端参数是否合法");
        //前端参数校验
        if (pageRequest == null
                || pageRequest.getCurrentPage() == null
                || pageRequest.getPageSize() == null) {
            return Result.invalidParameter();
        }

        //调用service进行逻辑判断
        PageResponse<BookInfo> pageResponse;
        try {
            pageResponse=bookService.getListByPage(pageRequest);

        } catch (Exception e) {
            log.error("数据库查询失败：{}",e);
            return Result.fail();
        }
        log.info("查询成功");
        return Result.success(pageResponse);
    }

    //更新图书
    @RequestMapping(value="/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {

        //参数校验
        if(bookInfo==null||bookInfo.getId()==null){
            return "前端参数对象为空或者id为空";
        }
        return bookService.updateBook(bookInfo);
    }

    //根据id查询图书信息
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("获取图书信息，通过id："+bookId);

        //校验参数 省略

        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value="/deleteBook",produces = "application/json")
    public String deleteBookById(Integer bookId){
        if(bookId==null){
            return "前端参数传递为null";
        }
        return bookService.deleteBookById(bookId);
    }


    @RequestMapping(value="/batchDeleteBook",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
            if(ids==null){
                return "前端传递的ids=null";
            }
            return bookService.batchDelete(ids);
    }

}
